//
//  WBHomeViewController.swift
//  weibo
//
//  Created by 林帆 on 2017/3/10.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit

// 全局常量，
fileprivate let cellId = "WBStatusCell"

class WBHomeViewController: WBBaseViewController {

    //列表视图模型
    fileprivate lazy var listviewModel = WBStatusList_ViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
    }
    
     override func loadData() {
        
        listviewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            print("刷新表格")
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            
            //刷新表格
            if shouldRefresh {
                self.tableview?.reloadData()
            }
            
        }
    }
//        print("开始加载数据\(WBNetworkManager.shared)")
        
//        // 模拟‘延时’加载数据  ->  dispatch_after
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
//            for i  in 0..<15 {
//                if self.isPullup {
//                     //将数据追加到底部
//                    self.statusList.append("上拉  \(i)")
//                } else {
//                self.statusList.insert(i.description, at: 0)
//                }
//            }
//            print("刷新表格")
//            self.refreshControl?.endRefreshing()
//            self.isPullup = false
//            self.tableview?.reloadData()
//        }
  
    
    //显示好友
    @objc  func showFriends() {
        print(#function)
        let vc = WBDemoViewController()
        
        // push 的动作是 nav 做的
        navigationController?.pushViewController(vc, animated: true)
        
    }

}

// MARK: - 数据
extension WBHomeViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listviewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1. 取cell
        let cell = tableview?.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        //2. 设置cell
        //1.
        cell.MaintextLabel.text = listviewModel.statusList[indexPath.row].text
        //3. 返回cell
        return cell
        
    }
   
}

// MARK: - 设置界面
extension WBHomeViewController {
    override func setupTableView() {
        super.setupTableView()
        // 设置导航栏按钮  
        navItem.leftBarButtonItem = UIBarButtonItem(title: "笨蛋", fontSize: 16, width: 40, target: self, action: #selector(showFriends))
        
        //注册原型 cell
        tableview?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellId)
        tableview?.rowHeight = UITableViewAutomaticDimension
        tableview?.estimatedRowHeight = 300
        
        //取消分割线
        tableview?.separatorStyle = .none
        setupNavTitle()
    }
    
    fileprivate func setupNavTitle( ) {
        
        let title = "=="
        let button = WBTitleButton(title: title)
        navItem.titleView = button
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        
    }
    
    @objc func clickTitleButton(btn : UIButton) {
        // 设置选中状态
        btn.isSelected = !btn.isSelected
    }

 
}
